package com.nf.controller.manage;

import com.google.gson.Gson;
import com.nf.controller.BaseServlet;
import com.nf.dao.ShowInfoDao;
import com.nf.dao.impl.ShowInfoDaoImpl;
import com.nf.util.GsonUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * 编写servlet，实现修改放映计划
 */
@WebServlet("/api/UpdateShowServlet.do")
public class UpdateShowServlet extends BaseServlet {
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        BufferedReader reader=req.getReader();
        String str=null;
        String jsonParams="";
        while ((str=reader.readLine())!=null){
            jsonParams=jsonParams+str;
        }
        Map<String,String> map= GsonUtil.parseMap(jsonParams);

        String showDate=map.get("showDate");
        String startTime=map.get("startTime");
        String endTime=map.get("endTime");
        Float price= Float.valueOf(map.get("price"));
        int id= Integer.parseInt(map.get("showId"));

        ShowInfoDao dao=new ShowInfoDaoImpl();
        int r= dao.update(showDate,startTime,endTime,price,id);

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        if (r==1){
            out.print(new Gson().toJson(successJson("修改成功")));
        }else{
            out.print(new Gson().toJson(errorJson("修改失败")));
        }
        out.flush();
        out.close();
    }
}
